package interfaz2;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

/**
 * This class is a Label that can be configure the type of Font used for each
 * of its letters.
 * 
 * Although it seems to subclass the Label class it doesn't since SWT doesn't
 * permit it, it extends the DustComposite.
 * 
 * @author ole
 */
public class MultiFontLabel extends DustComposite {
	private Label labels[];
	private String text;
	
	/**
	 * Class constructor.
	 * @param parent:
	 * 		The Composite parent.
	 */
	public MultiFontLabel(Composite parent) {
		super(parent, SWT.NONE);
		labels = null;
		text = null;
	}
	
	/**
	 * This method is used to quickly configure the style of each letter of
	 * the text attached to this class.
	 * 
	 * Right now this is quite simple, it can be modified adding functionality
	 * if needed, I'm still amused that SWT haven't something like that... GTK
	 * has it...
	 * 
	 * The string passed is expected to be of the same length of the text
	 * attached to this instance. This means you first need to use the
	 * setText() method. The string can has any characters but the underscores
	 * will be interpreted. The underscore means that the character at the same
	 * position as the underscore must be bold and any other character means
	 * that the character at the same position must be normal.
	 * 
	 * @param style:
	 * 		A string of the format described, indicating the style of each
	 * 		letter.
	 */
	public void setStyle(String style) {
		Character symbol;
		Font font;
		FontData font_data;
		Label label;
		
		if (text == null || text.length() != style.length())
			return;
		
		for (int i = 0; i < text.length(); i++) {
			symbol = style.charAt(i);
			
			// If this symbol is not '_' just ignore it.
			if (symbol != '_')
				continue;
			
			// We have found a '_' so change the FontData to be
			// bold.
			label = labels[i];
			font = label.getFont();
			font_data = font.getFontData()[0];
			font_data.setStyle(SWT.BOLD);
			font = new Font(getDisplay(), font_data);
			label.setFont(font);
		}
	}
	
	/**
	 * Set the text of the Label.
	 * 
	 * @param text:
	 * 		A string with the text to be set.
	 */
	public void setText(String text) {
		Character letter;
		GridLayout layout;
		
		// Create the layout and set the values needed to make this Composite
		// seems a Label.
		layout = new GridLayout(text.length(), false);
		layout.horizontalSpacing = 0;
		layout.verticalSpacing = 0;
		layout.marginBottom = 0;
		layout.marginHeight = 0;
		layout.marginLeft = 0;
		layout.marginRight = 0;
		layout.marginTop = 0;
		layout.marginWidth = 0;
		this.setLayout(layout);
		
		// Initialize the array of labels.
		labels = new Label[text.length()];
		
		// Create a new Label for each text character.
		for (int i = 0; i < text.length(); i++) {
			letter = text.charAt(i);
			labels[i] = new Label(this, SWT.NONE);
			labels[i].setText(letter.toString());
		}
		
		this.text = text;
	}
}
